package com.example.administrator.testc.DBs;/**
 * Created by Administrator on 2016/6/6.
 */

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.administrator.testc.mApp;

/**
 * >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>类描述
 * >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>属性描述
 * >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>方法描述
 */
public class ServiceData {

    static ServiceData instance;

    private static DB database;

    private ServiceData(){}

    public static void initDB(){
        database = new DB(mApp.getContext());
    }

    public static synchronized ServiceData getInstance() {
        if(instance == null)
            instance = new ServiceData();
        return instance;
    }

    public static AdvertisingBean queryAdvertising(){
        SQLiteDatabase db = database.getWritableDatabase();
        AdvertisingBean bean = new AdvertisingBean();
        if(db.isOpen()){
            Cursor cursor = db.rawQuery("select * from " + AdvertisingBean.EAdvertising.TABEL_NAME.getValue(), null);
            while (cursor.moveToNext()){
                bean.setClickUrl(cursor.getString(cursor.getColumnIndex(AdvertisingBean.EAdvertising.clickUrl.name())));
                bean.setFile(cursor.getString(cursor.getColumnIndex(AdvertisingBean.EAdvertising.file.name())));
                bean.setHeight(cursor.getInt(cursor.getColumnIndex(AdvertisingBean.EAdvertising.height.name())));
                bean.setId(cursor.getInt(cursor.getColumnIndex(AdvertisingBean.EAdvertising.id.name())));
                bean.setPageId(cursor.getInt(cursor.getColumnIndex(AdvertisingBean.EAdvertising.pageId.name())));
                bean.setPositionId(cursor.getInt(cursor.getColumnIndex(AdvertisingBean.EAdvertising.positionId.name())));
                bean.setPubDates(cursor.getString(cursor.getColumnIndex(AdvertisingBean.EAdvertising.pubDates.name())));
                bean.setWidth(cursor.getInt(cursor.getColumnIndex(AdvertisingBean.EAdvertising.width.name())));
                //Log.i("TAGSS", "querybean is " + bean.toString());
            }
        }
        return bean;
    }

    public static boolean SaveAdvertising(AdvertisingBean bean){
        SQLiteDatabase db = database.getWritableDatabase();
        if(queryAdvertising() != null){
            db.delete(AdvertisingBean.EAdvertising.TABEL_NAME.getValue(), null, null);
        }
        Long Id_ = db.replace(AdvertisingBean.EAdvertising.TABEL_NAME.getValue(),
                null, AdvertisingBean.EAdvertising.getContentValues(bean));
        if(Id_ != -1)
            return true;
        return false;
    }

    /**
     * 更新用户基础信息
     * @param bean
     */
    public static synchronized boolean updateAdvertising(AdvertisingBean bean) {
        SQLiteDatabase db = database.getWritableDatabase();
        if (db.isOpen()) {
            Long Id_ = db.replace(AdvertisingBean.EAdvertising.TABEL_NAME.getValue(),
                    null, AdvertisingBean.EAdvertising.getContentValues(bean));
            if (Id_ != -1)
                return true;
        }
        return false;
    }
}
